﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace Nirvana
{
    [CustomEditor(typeof(AssetCheckRule))]
    public class AssetCheckRuleEditor : Editor
    {
        private int startindex = -1;
        private Vector2 pos;

        private SerializedProperty includeRegexs;
        private SerializedProperty excludeRegexs;
        private SerializedProperty checkModel;
        private SerializedProperty checkAnimation;
        private SerializedProperty checkTexture;
        private SerializedProperty checkStanderdMaterial;
        private SerializedProperty checkMaterial;
        private SerializedProperty checkAudio;
        private SerializedProperty checkRenderer;
        private SerializedProperty checkParticleSystem;
        private SerializedProperty checkUI;


        public override void OnInspectorGUI()
        {
            base.serializedObject.Update();

            GUI.color = Color.cyan;
            EditorGUILayout.HelpBox("排查目录下的资源，配置选项是否开启/关闭，避免资源未设置正确而没优化。" +
                "", MessageType.Info);
            GUI.color = Color.white;
            EditorGUILayout.Space();

            GUILayout.Label("匹配规则", GUITheme.LabelTitleStyle);
            GUILayoutEx.BeginContents();
            GUI.color = Color.yellow;
            EditorGUILayout.HelpBox("注意：规则文件只会检测同一父目录或同一级目录下的资源。\n\n" +
                "正则例子：1.匹配非空字符：\\S , 2.任意字符：. ", MessageType.None);
            GUI.color = Color.white;

#if UNITY_2019_1_OR_NEWER
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.includeRegexs);
            EditorGUILayout.PropertyField(this.excludeRegexs);
            GUILayoutEx.EndContents();
            EditorGUILayout.Space();

			EditorGUILayout.PropertyField(this.checkModel);
            EditorGUILayout.PropertyField(this.checkAnimation);
            EditorGUILayout.PropertyField(this.checkTexture);
            EditorGUILayout.PropertyField(this.checkMaterial);
            EditorGUILayout.PropertyField(this.checkStanderdMaterial);
            EditorGUILayout.PropertyField(this.checkAudio);
            EditorGUILayout.PropertyField(this.checkRenderer);
            EditorGUILayout.PropertyField(this.checkParticleSystem);
            EditorGUILayout.PropertyField(this.checkUI); 
#else
            GUILayoutEx.EndContents();
            EditorGUILayout.Space();
            base.DrawDefaultInspector();
#endif




            AssetCheckRule assetCheckRule = (AssetCheckRule)this.target;
            EditorGUILayout.Space(10f);
            if (GUILayoutEx.ActionButton("Check"))
            {
                using (ProgressIndicator progressIndicator = new ProgressIndicator("Check Assets"))
                {
                    progressIndicator.SetTotal((float)assetCheckRule.GetTotalCount());
                    assetCheckRule.Progress(progressIndicator);
                  //  EditorUtility.SetDirty(this.target);
                }
                GUIUtility.ExitGUI();
            }

            var list = assetCheckRule.Report.Reports;
            if (list.Count > 0)
            {
                EditorGUILayout.PrefixLabel(string.Format("Report({0}):", list.Count));
                AssetCheckReportItem.Show(list, ref this.pos, ref this.startindex);
            }

            serializedObject.ApplyModifiedProperties();
        }

        private void OnEnable()
        {
            if (this.target == null)
            {
                return;
            }
            SerializedObject serializedObject = base.serializedObject;
            this.includeRegexs = serializedObject.FindProperty("includeRegexs");
            this.excludeRegexs = serializedObject.FindProperty("excludeRegexs");
            this.checkModel = serializedObject.FindProperty("checkModel");
            this.checkAnimation = serializedObject.FindProperty("checkAnimation");
            this.checkTexture = serializedObject.FindProperty("checkTexture");
            this.checkMaterial = serializedObject.FindProperty("checkMaterial");
            this.checkStanderdMaterial = serializedObject.FindProperty("checkStanderdMaterial");
            
            this.checkAudio = serializedObject.FindProperty("checkAudio");
            this.checkRenderer = serializedObject.FindProperty("checkRenderer");
            this.checkParticleSystem = serializedObject.FindProperty("checkParticleSystem");
            this.checkUI = serializedObject.FindProperty("checkUI");

        }


    }
}

